package com.lanzhou.yuanfen.controller;

import com.alibaba.fastjson.JSONObject;
import com.lanzhou.yuanfen.cache.ArticleCacheService;
import com.lanzhou.yuanfen.diary.entity.Article;
import com.lanzhou.yuanfen.diary.service.IArticleService;
import com.lanzhou.yuanfen.response.ServerResponseResult;
import com.lanzhou.yuanfen.security.session.GrantedAuthorityService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/test/")
public class TestController {

    @Resource
    GrantedAuthorityService grantedAuthorityService;

    @Resource
    private IArticleService articleService;

    @Resource
    private ArticleCacheService articleCacheService;


    @GetMapping("runAs")
    public ServerResponseResult runAs(HttpServletRequest request) {
        HttpSession session = request.getSession();
        grantedAuthorityService.reloadUserAuthority(session, 6L);
        return ServerResponseResult.success();
    }


    @GetMapping("redis")
    public ServerResponseResult redis() {
        List<Article> list = articleService.redis();
        System.out.println(list);
        return ServerResponseResult.success(list);
    }

    @GetMapping("redisTest")
    public ServerResponseResult redisTest() {
        articleCacheService.initArticle();
        return ServerResponseResult.success();
    }


    @GetMapping("redis01")
    public ServerResponseResult redis01() {
        Set<Long> article = articleCacheService.getArticle(9L);
        return ServerResponseResult.success(article);
    }

    @GetMapping("redis02")
    public ServerResponseResult redis02() {
        Map<Long, Set<Long>> article = articleCacheService.pageGetArticle(9L, 1, 1);
        return ServerResponseResult.success(article);
    }

    @GetMapping("test01")
    public ServerResponseResult test01(HttpServletRequest request) {
        String url = request.getParameter("url");
        File file = new File(url);
        System.out.println(JSONObject.toJSONString(file));
        System.out.println(file.exists());

        String contextPath = request.getServletContext().getContextPath();
        System.out.println(contextPath);
        return ServerResponseResult.success();
    }


    /*private static CellType[] type = {CellType.STRING, CellType.STRING, CellType.STRING, CellType.STRING, CellType.STRING, CellType.STRING};


    private static final String exportPath1 = "C:\\Users\\Administrator\\Desktop\\xzq_201907.xlsx";


    @GetMapping("/getCityJSON")
    anonymous List<Map<String, Object>> getCityJSON() {
        Map<String, String> importFiled = ImportFiled.getImportFiled();
        String[] key = new String[importFiled.keySet().size()];
        String[] values = new String[importFiled.keySet().size()];
        Iterator<Map.Entry<String, String>> iterator = importFiled.entrySet().iterator();
        int index = 0;
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            key[index] = next.getValue();
            values[index] = next.getKey();
            index++;
        }
        List<Map<String, Object>> mapList = ExcelRead.readEcxcel(new File(exportPath1), key, values, type);
        return mapList;
    }*/


}
